package zzxkj.blog.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zzxkj.blog.imChat.handler.MsgHandlerFactory;
import zzxkj.blog.imChat.server.ImServerStarter;

import java.io.IOException;

/**
 * 启动tio，并初始化消息处理器
 * @author zzxkj
 */
@Slf4j
@Configuration
public class ImServerConfig {
    /**
     * 端口
     * 单机不需要ip
     */
    @Value("${im.server.port}")
    private int imPort;

    @Bean
    ImServerStarter imServerStarter() {
        try {
            // 启动tio服务，连接
            ImServerStarter serverStarter = new ImServerStarter(imPort);
            serverStarter.start();

            // 初始化消息处理器类别
            MsgHandlerFactory.init();

            return serverStarter;
        } catch (IOException e) {
            log.error("tio server 启动失败", e);
        }

        return null;
    }
}